home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / genial / func / flood.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  3.0 KB  |  129 lines

  1. /*
  2.  * regions/flood.c -- routines for doing flood fills on a region
  3.  * this is some of the guliest code in all of GENIAL.
  4.  */
  5.  
  6. #include "reg.h"
  7. #include "common.h"
  8. #include "display.h"
  9. #include "ui.h"
  10. #include <X11/Xlib.h>
  11. #include <stdio.h>
  12.  
  13. static XImage *pfrm = NULL;    /* psuedo frame buffer.  perverse, but
  14.                  * expedient */
  15.  
  16. /* this could be done a LOT more efficiently */
  17. #define RLEV 150000        /* should be plenty */
  18.  
  19. int
  20. flood_fill(reg, pbuf, size, len)
  21.     struct region *reg;
  22.     XPoint  **pbuf;
  23.     int      *size, *len;
  24. {
  25.     u_char   *tmp;
  26.     static XPoint *stack = NULL;
  27.     static int hiwat = 0;
  28.     int       dpt = 0;        /* depth into the stack */
  29.     register unsigned long v;
  30.     int       x, y;
  31.     int       pt_cnt = 0;    /* count of # of points found */
  32.     int       sx, sy, n;
  33.     char     *realloc();
  34.     struct plist *trav;
  35.  
  36.     if (pfrm != NULL) {
  37.     XDestroyImage(pfrm);
  38.     pfrm = NULL;
  39.     }
  40.     /*
  41.      * create a zero'ed XImage which will just store points where the polygon
  42.      * is.
  43.      */
  44.     tmp = (unsigned char *) calloc(1, (orig_ximg->width * orig_ximg->height));
  45.     pfrm = XCreateImage(display, winv, depth, ZPixmap,
  46.             0, tmp, orig_ximg->width, orig_ximg->height, 8, 0);
  47.  
  48.     /*
  49.      * place all the points associated with the polygon or spline into the
  50.      * psuedo frame buffer
  51.      */
  52.     drawpts(reg);
  53.  
  54.     /* initialize sx and sy */
  55.     sx = sy = n = 0;
  56.     for (trav = reg->r_plist; trav != NULL; trav = trav->next) {
  57.     sx += trav->pt.x;
  58.     sy += trav->pt.y;
  59.     n++;
  60.     }
  61.  
  62.     sx = sx / n;
  63.     sy = sy / n;
  64.     if (hiwat == 0) {
  65.     hiwat += RLEV;
  66.     stack = (XPoint *) malloc(hiwat * sizeof(XPoint));
  67.     }
  68.     stack[0].x = sx;
  69.     stack[0].y = sy;
  70.     while (dpt >= 0) {
  71.     x = stack[dpt].x;
  72.     y = stack[dpt].y;
  73.     v = (unsigned long) XGetPixel(pfrm, x, y);
  74.     if (v == (unsigned long) standout) {
  75.         dpt--;
  76.     } else {
  77. #ifdef SHOWFF
  78.         XDrawPoint(display, img_win->d_xid, gc, x, y);
  79. #endif
  80.         XPutPixel(pfrm, x, y, (unsigned long) standout);
  81.         pt_cnt++;
  82.         /* add the point to the point buffer */
  83.         if (*len >= *size) {
  84.         *size += BSIZE;
  85.         *pbuf = (XPoint *) realloc(*pbuf, (*size) * sizeof(XPoint));
  86.         }
  87.         (*pbuf)[*len].x = x;
  88.         (*pbuf)[*len].y = y;
  89.         (*len)++;
  90.         if (dpt + 4 >= hiwat) {
  91.         printf("reallocing the stack \n");
  92.         hiwat += RLEV;
  93.         stack = (XPoint *) realloc(stack, hiwat * sizeof(XPoint));
  94.         }
  95.         ++dpt;
  96.         stack[dpt].x = x - 1;
  97.         stack[dpt].y = y;
  98.         ++dpt;
  99.         stack[dpt].x = x + 1;
  100.         stack[dpt].y = y;
  101.         ++dpt;
  102.         stack[dpt].x = x;
  103.         stack[dpt].y = y - 1;
  104.         ++dpt;
  105.         stack[dpt].x = x;
  106.         stack[dpt].y = y + 1;
  107.     }
  108.     }
  109.     return pt_cnt;
  110. }
  111.  
  112.  
  113. drawpts(reg)
  114.     struct region *reg;
  115. {
  116.     struct dlist *pstore;
  117.     XGCValues gcval;
  118.     int       i;
  119.  
  120.     gcval.foreground = standout;
  121.     XChangeGC(display, gc, GCForeground, &gcval);
  122.     for (pstore = reg->r_dlist; pstore != NULL; pstore = pstore->next) {
  123.     for (i = 0; i < pstore->len; i++) {
  124.         XPutPixel(pfrm, (int) pstore->points[i].pt.x,
  125.         (int) pstore->points[i].pt.y, (unsigned long) standout);
  126.     }
  127.     }
  128. }
  129.